package com.springboot.demo.mapper;

import com.springboot.demo.entity.Menu;
import com.springboot.demo.entity.MenuGroup;
import com.springboot.demo.entity.Role;
import com.springboot.demo.utils.Constant;
import io.swagger.annotations.ApiModelProperty;
import org.apache.ibatis.annotations.*;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Set;

/**
 * @author wubc683
 * @desc
 * @date 2021-08-16 14:54
 */
public interface MenuMapper {

    @Select("select m.id as id,m.url as url,m.method as method,m.groupId as groupId,#{adminId} as adminId from " +
            "web_menu m left join web_role_menu_group rmg on rmg.menuGroupId = m.groupId ")
    @Results({// @Results：封装映射关系的父注解
            @Result(column = "id", property = "id"), //@Result：封装映射关系的子注解
            @Result(column = "url", property = "url"),
            //column 属性：查询出的表中字段名称
            //property 属性：实体对象中的属性名称
            @Result(property = "roles",
                    javaType = List.class,
                    //javaType 属性：被包含对象的数据类型
                    column = "{menuGroupId=groupId,adminId=adminId}",
                    many = @Many(select = "com.springboot.demo.mapper.RoleMapper.getRolesByMenuGroupIdAndAdminId")
            )//many 属性：一对多查询固定属性
    })
    List<Menu> getAllMenuRolesByAdminId(Long adminId);

    @Delete("delete from web_role_menu_group where roleId = #{rid}")
    void deleteMenuGroupByRid(Long rid);

    @Select("select mg.id,mg.name,mg.parentId,mg.onlyAdmin,mg.preMenuId,mg.canFold ,rmg.selected from " +
            "web_menu_group mg,web_role_menu_group " +
            "rmg where mg.id = rmg.menuGroupId " +
            "and rmg.roleId = #{rid}")
    List<MenuGroup> getMenuGroupByRid(Long rid);

    @Select("select id from web_menu_group where id in(select menuGroupId from web_role_menu_group where roleId " +
            "=#{rid}) ")
    List<Long> getAllMenuGroupIdsByRid(Long rid);

    @Insert("<script> insert into web_role_menu_group(roleId,menuGroupId,selected) values <foreach item='item' " +
            "collection='menuGroups' separator=','> (#{rid},#{item.menuGroupId},#{item.selected}) </foreach> </script>")
    void saveRoleMenuGroups(Long rid, List<MenuGroup> menuGroups);

    @Insert("<script> insert into web_role_menu_group(roleId,menuGroupId,selected) values <foreach item='item' " +
            "collection='menuGroupByRid' separator=','> (#{rid},#{item.id},#{menuNotSelected}) </foreach> " +
            "</script>")
    void insertRoleMenuGroup(Long rid, List<MenuGroup> menuGroupByRid, Integer menuNotSelected);

    @Update("<script> <foreach item='item' collection='menuGroups' separator=';'> update web_role_menu_group set " +
            "selected = #{item.selected} where menuGroupId = #{item.id} and roleId = " +
            "#{rid} </foreach> </script>")
    void updateRoleMenuGroups(Long rid, List<MenuGroup> menuGroups);

    @Select("select rmg.roleId from web_role_menu_group rmg inner join web_menu m on rmg.menuGroupId = m.groupId " +
            "where rmg" + ".selected = 1 and m.url = #{url} and m.method = #{method}")
    List<Long> getMenuAllRolesByUrlAndMethod(String url, String method);

    @Select("select id,url,method ,name from web_menu")
    List<Menu> getAllMenus();

    @Select("select m.id,m.url,m.method from web_menu m inner join web_role_menu_group rmg on m.groupId = rmg" +
            ".menuGroupId where rmg.roleId = #{rid} and rmg.selected = #{selected}")
    List<Menu> getMenuByRid(Long rid, Integer selected);
}
